package cn.action;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.domain.Cate;
import cn.domain.User;
import cn.util.Pagination;
import cn.util.Util;

/**
 * 
 * @项目名称:pigbaby
 * @类名称:ApkAction
 * @类描述:移动设备的所有接口
 * @创建人:陈凯
 * @创建时间:20150713 17：23 星期一
 * @修改人:
 * @修改时间:
 * 
 * @修改备注:
 * @version
 */
@Namespace("/")
@Scope("prototype")
@ParentPackage("json-default")
@Component("cateAction")
@Action(value = "cateAction", results = { 
		@Result(name = "loginout", location = "/login.jsp"),
		@Result(name = "cate", location = "/cate.jsp"),		
		@Result(name = "resultjson", type = "json")
		})
public class CateAction extends BaseAction {

	// 保存日志
	public static Logger logger = LoggerFactory.getLogger(CateAction.class);
	private String deleteId;
	private Cate cate;

	public String cate(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		return "cate";
	}
	
	public String destroy(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		if(deleteId != null && !"".equals(deleteId)){
			cateService.delete(deleteId);
		}
		outJSONMessage("success");
		return "resultjson";
	}

	public String insert(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		if(cateService.getOne(cate)){
			outJSONMessage("该模块已经存在！");
		}else{
			cate.setAddtime(Util.DateToInt(new Date()));
			cate.setIsdel(0);
			cateService.insert(cate);
			outJSONMessage("success");
		}
		return "resultjson";
	}
	
	public String list() throws UnsupportedEncodingException{
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		String pagestr = request.getParameter("page");
		pageIndex = Integer.parseInt(pagestr);
		String search = "";
		if(cate.getCname() != null && !"".equals(cate.getCname())){
			search = new String(cate.getCname().getBytes("iso8859-1"),"UTF-8");
		}
		total = cateService.getAllCount(cate);
		pagination = new Pagination(pageIndex,rows,total);
		pagination.setId(cate.getPid());
		pagination.setSearch(search);
		List<Cate> list = cateService.getAlllist(pagination);
		outJSONList(list,total);
		return "resultjson";
	}

	public String edit(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		if(deleteId != null && !"".equals(deleteId)){
			cate = cateService.selectOne(deleteId);
		}
		outJSONObject(cate);
		return "resultjson";
	}
	
	public String update(){
		User user = (User)request.getSession().getAttribute("user");
		if(user == null){
			return "loginout";
		}
		Cate cate_db = cateService.selectOne(cate.getAid()+"");
		if(!cate_db.getCname().equals(cate.getCname())){
			if(cateService.getOne(cate)){
				outJSONMessage("该模块已经存在！");
			}else{
				cate_db.setCname(cate.getCname());
				cate_db.setCdesc(cate.getCdesc());
				cateService.update(cate_db);
				outJSONMessage("success");
			}
		}else {
			cate_db.setCname(cate.getCname());
			cate_db.setCdesc(cate.getCdesc());
			cateService.update(cate_db);
			outJSONMessage("success");
		}
		return "resultjson";
	}
	
	
	
	public String getDeleteId() {
		return deleteId;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

	public Cate getCate() {
		return cate;
	}

	public void setCate(Cate cate) {
		this.cate = cate;
	}
	
}
